﻿// 配置全局接口域名
import router from "./router";

if(sessionStorage.getItem("username")==null){
// sessionStorage.setItem("companyid","1");   
// sessionStorage.setItem("sid","admin1231592357268645");
// sessionStorage.setItem("username","admin123");
// sessionStorage.setItem("roleId","1");                        
 }

//var ip="http://58.61.154.225:8107";
var ip="http://106.53.73.88:8107";
if(location.href.toString().indexOf("lgsvtest.learngun.com.cn")==-1){
  ip="http://192.168.1.35:8107";
}

//ip="http://119.23.148.39:8107";
//if(location.href.toString().indexOf("http://180.76.245.167:9019")==-1){
// if(location.href.toString().indexOf("lgsvtest.learngun.com.cn")==-1){
//  // if(location.href.toString().indexOf("119.23.148.39:8107")==-1){
//  // ip="http://180.76.245.167:9019";
//  var ip="http://192.168.1.35:8107";
// }
const domain=ip;
//const domain='http://192.168.1.134:8080';
// 判断sid是否过期
if(sessionStorage.getItem("sid")==null){
swal("OMG!", "登录过期！", "error");
// alert("登录过期！"); 

// location.href="/test/dist/index.html#/Login"; 
location.href="#/Login"; 
}
//const domain='http://192.168.1.207:8080';
const staticif="1";
var ipseve="http://106.53.73.88:8107";
//ipseve="http://119.23.148.39:8107";
//ipseve="http://180.76.245.167:9019";
//if(location.href.toString().indexOf("http://180.76.245.167:9019")==-1){
// if(location.href.toString().indexOf("192.168")!=-1){
  if(location.href.toString().indexOf("lgsvtest.learngun.com.cn")==-1){
 ipseve="http://192.168.1.35:8107";
 }

export default {
  testUrl:domain,
  ajaxUrl:ajax,
  staticzt:staticif,
  jsonthis:"",
  fieldthis:field,
  ipimg:ipseve,
  fieldupdatethis:fieldupdate,
  language:"", 
  
}
//添加
var AZ=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
function field(fieldvalue,val,table){
 var value="";
// console.log(fieldvalue);
 var _list = {};
 _list["tableName"]=table;
  for(var i=0;i<fieldvalue.length;i++){
    _list["fieldname"+AZ[i]]= fieldvalue[i];
    //value+="fieldname"+AZ[i]+":"+fieldvalue[i]+",";
  }
  for(var i=0;i<val.length;i++){
    _list["stringvalue"+AZ[i]]= val[i];
    //value+="fieldname"+AZ[i]+":"+fieldvalue[i]+",";
  }
  //value = value.substring(0, value.length-1);
  //console.log(value);
  return _list;
}
//修改
function fieldupdate(fieldvalue,val,fielded,valueid,table){
  var value="";
 // console.log(fieldvalue);
  var _list = {};
  _list["tableName"]=table;
   for(var i=0;i<fieldvalue.length;i++){
     _list["fieldname"+AZ[i]]= fieldvalue[i];
     //value+="fieldname"+AZ[i]+":"+fieldvalue[i]+",";
   }
   for(var i=0;i<val.length;i++){
     _list["stringvalue"+AZ[i]]= val[i];
     //value+="fieldname"+AZ[i]+":"+fieldvalue[i]+",";
   }
   _list["fieldid"]=fielded;
   _list["valueid"]=valueid;
   //value = value.substring(0, value.length-1);
   //console.log(value);
   return _list;
 }

/* 封装ajax函数
 * @param {string}opt.type http连接的方式，包括POST和GET两种方式
 * @param {string}opt.url 发送请求的url
 * @param {boolean}opt.async 是否为异步请求，true为异步的，false为同步的
 * @param {object}opt.data 发送的参数，格式为对象类型
 * @param {function}opt.success ajax发送并接收成功调用的回调函数
 */
function ajax(opt){
  opt = opt || {};
  opt.method = opt.method.toUpperCase() || 'POST';
  opt.url = opt.url || '';
  opt.async = opt.async || true;
  opt.data = opt.data || null;
  opt.success = opt.success || function () {};
  var xmlHttp = null;
  if (XMLHttpRequest) {
      xmlHttp = new XMLHttpRequest();
  }
  else {
      xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  }var params = [];
  for (var key in opt.data){
      params.push(key + '=' + opt.data[key]);
  }
  var postData = params.join('&');
  if (opt.method.toUpperCase() === 'POST') {
    try{
      xmlHttp.open(opt.method, opt.url, opt.async);
      xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
      xmlHttp.send(postData);
    }catch(err){
      swal("OMG!", "请求失败", "error");
    }
  }
  else if (opt.method.toUpperCase() === 'GET') {
      xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async);
      xmlHttp.send(null);
  } 
  xmlHttp.onreadystatechange = function () {
      if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
    
       // console.log(xmlHttp.readyState);   console.log(xmlHttp.status);
       try{
       if(JSON.parse(xmlHttp.responseText).data=="登入过期"){
        swal("OMG!", "登录过期！", "error");
         //alert("登录过期！"); 
        setTimeout(function(){
          try{
     // parent.location.href="/test/dist/index.html#/Login"; 
          }catch(ex){
         //  location.href="/test/dist/index.html#/Login"; 
          }
        },0);
        return;
       }
       else{
      // sessionStorage.setItem("sid",sid)
        opt.success(xmlHttp.responseText);
       }
      }catch(ex){
        opt.success(xmlHttp.responseText);
      }
      }
  }
}
